package com.lul15834.orderfood.activity.manager.fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;

import androidx.appcompat.widget.SearchView;
import androidx.fragment.app.Fragment;
import com.lul15834.orderfood.R;
import com.lul15834.orderfood.activity.manager.adapter.FoodListAdapter;
import com.lul15834.orderfood.bean.FoodBean;
import com.lul15834.orderfood.dao.FoodDao;
import com.lul15834.orderfood.until.Tools;

import java.util.List;


public class ManagerHomeFragment extends Fragment {
    View rootview;//根视图

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        rootview=inflater.inflate(R.layout.fragment_manager_home, container, false);
        //适配器
        ListView listView=rootview.findViewById(R.id.man_home_food_list);
        List<FoodBean> list= FoodDao.getAllFoodById(Tools.getOnAccount(getContext()));
        FoodListAdapter adapter=new FoodListAdapter(getContext(),list);
        if (list==null||list.size()==0){
            listView.setAdapter(null);
        }else {
            listView.setAdapter(adapter);
        }
        String account= Tools.getOnAccount(getContext());
        SearchView searchView= rootview.findViewById(R.id.man_home_food_search);
        searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String query) {
                //根据标题来进行查找商品
                List<FoodBean> list=FoodDao.getAllFoodList(account,query);
                FoodListAdapter adapter=new FoodListAdapter(getContext(),list);
                if (list==null||list.size()==0){
                    listView.setAdapter(null);
                }else {
                    listView.setAdapter(adapter);
                }

                return false;
            }

            @Override
            public boolean onQueryTextChange(String newText) {

                List<FoodBean> list=FoodDao.getAllFoodList(account,newText);
                FoodListAdapter adapter=new FoodListAdapter(getContext(),list);
                if (list==null||list.size()==0){
                    listView.setAdapter(null);
                }else {
                    listView.setAdapter(adapter);
                }return false;
            }
        });
        return rootview;
    }


}